package com.demo.crawler;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Crawler {

	private List<String> Album = new ArrayList<String>();
	private static Integer start = 0;
	private static Integer end = 100;

	/**
	 * 解析首页
	 *
	 * @param url
	 * @return
	 */

	public List<String> parseHomePage(String url) {

//		System.out.println("---------------------------------------开始解析首页---------------------------------------");
		List<String> urlsList = new ArrayList<String>();
		try {
			Document doc = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1").get();
			// 获取首页链接
			Elements links = doc.select(".article-content p");
//			for (Element link : links) {
//				String linkHref = link.attr("abs:href");
//				System.out.println(" ====> 首页获取到的页面url地址:" + linkHref);
//				urlsList.add(linkHref);
//			}
			++start;
			System.out.println("解析第" + start + "个页面");
			for (Element element : links) {
				System.out.println(element.text().contains("书库推荐") ? "" : element.text());
				readContextToFile("d:\\危险的维纳斯_[日]东野圭吾.txt",element.text().contains("书库推荐") ? "" : element.text());
			}
			// 获取首页中下一页按钮
			Elements nexts = doc.select("ul a:contains(下一页)");

			Element first = nexts.first();
			nexts.clear();
			nexts.add(first);

			for (Element next : nexts) {
				String linkHref = next.attr("abs:href");
				urlsList.add(linkHref);
//				System.out.println("---------------------------------------爬取第"+start+"页完成，稍后获取图片资源---------------------------------");
				if (start == end) {
					Thread thread = Thread.currentThread();
					thread.sleep(500);
//					System.out.println("---------------------------------------爬取前"+end+"页完成，稍后获取图片资源---------------------------------");
					continue;
				}
				if (linkHref == null || !url.equals(linkHref) || "".equals(linkHref)) {
					Thread thread = Thread.currentThread();
//					System.out.println("---------------------------------------正在获取下一页数据---------------------------------");
					thread.sleep(500);
					parseHomePage(linkHref);
				}
			}
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		Album.addAll(urlsList);
		return Album;
	}

	/**
	 * 解析图片详情页
	 *
	 * @param url
	 * @return
	 */
	public List<String> parseAlbumPage(String url) {
		List<String> Album = new ArrayList<String>();
		System.out.println("---------------------------------------开始解析图片页面---------------------------------");
		try {
			Document doc = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1").get();
			Elements links = doc.select("#bigImg");
			for (Element link : links) {
				String linkHref = link.attr("abs:src");
				Album.add(linkHref);
				System.out.println(" === >在页面：" + url + "获取到图片：" + linkHref);
			}
			Elements nexts = doc.select("#pageNext");
			for (Element next : nexts) {
				String linkHref = next.attr("abs:href");
				// 如果href=javascript:;.结束循环
				if (StringUtil.isBlank(linkHref)) {
					continue;
				}
				if (linkHref == null || !url.equals(linkHref) || "".equals(linkHref)) {
					Thread thread = Thread.currentThread();
					thread.sleep(500);
					Album.addAll(parseAlbumPage(linkHref));
				}
			}
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("---------------------------------------当前页面共获取图片" + Album.size() + "张---------------------------------");
		return Album;
	}

	public static void readContextToFile(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
